<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增文章')" />
	<th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-article-add">
			<div class="form-group">	
				<label class="col-sm-2 control-label">标题：</label>
				<div class="col-sm-10">
					<input id="articleTitle" name="articleTitle" class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label">选择图片：</div>
				<div class="col-sm-10">
					<div class="input-group">
						<input id='imgName'   class="form-control" onclick="$('#i-file').click();">
						<label class="input-group-btn">
							<input type="button" id="i-check" value="浏览图片" class="btn btn-primary" onclick="$('#i-file').click()">
						</label>
					</div>
				</div>
				<input type="hidden" name="imgPath">
				<input type="hidden" name="imgName">
				<input type="file" name="file" id='i-file'  accept=".jpg, .png, .jpeg, " onchange="uploadImage(this.files[0]);" style="display: none">
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">所属栏目：</label>
				<div class="col-sm-10">
					<select name="columnId" class="form-control m-b" >
						<option th:each="item : ${columnInfoList}" th:text="${item.columnName}" th:value="${item.columnId}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">简介：</label>
				<div class="col-sm-10">
					<textarea name="articleAbstract" maxlength="500" class="form-control" rows="3"></textarea>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-2 control-label">内容：</label>
				<div class="col-sm-10">
				    <input id="articleContent" name="articleContent" type="hidden">
				    <div class="summernote"></div>
				</div>
			</div>
		</form>
	</div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
        var prefix = "/sys/article";
	    $('.summernote').summernote({
	    	placeholder: '请输入文章内容',
			height : 192,
			lang : 'zh-CN',
			followingToolbar: false,
			callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
		});
	    
	    // 上传文件
	    function sendFile(file, obj) {
	        var data = new FormData();
	        data.append("file", file);
	        $.ajax({
	            type: "POST",
	            url: "/common/upload",
	            data: data,
	            cache: false,
	            contentType: false,
	            processData: false,
	            dataType: 'json',
	            success: function(data) {
	                if (data.state == web_status.SUCCESS) {
	                	$(obj).summernote('editor.insertImage', data.data.url, data.data.fileName);
					} else {
						$.modal.alertError(data.data.msg);
					}
	            },
	            error: function(error) {
	                $.modal.alertWarning("图片上传失败。");
	            }
	        });
	    }
		//上传图片
	    function uploadImage(file){
	    	$("#imgName").val(file.name);
			var data = new FormData();
			data.append("file", file);
			$.ajax({
				type: "POST",
				url: "/common/upload",
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				dataType: 'json',
				success: function(data) {
					if (data.state == web_status.SUCCESS) {
						$("input[name='imgPath']").val(data.data.url);
						$("input[name='imgName']").val(data.data.fileName);
					} else {
						$.modal.alertError(data.data.msg);
					}
				},
				error: function(error) {
					$.modal.alertWarning("图片上传失败。");
				}
			});
		}
		
		$("#form-article-add").validate({
			focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	var sHTML = $('.summernote').summernote('code');
				$("#articleContent").val(sHTML);
				$.operate.save(prefix + "/saveOrUpdate", $('#form-article-add').serialize());
	        }
	    }
	</script>
</body>
</html>
